package com.cndongtai.service.firmware;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.cndongtai.dao.impl.LightFirmwareDao;
import com.cndongtai.po.LightFirmware;
import com.cndongtai.util.DateUtil;
import com.cndongtai.vo.LightFirmwareBean;

@Service("lightFirmwareService")
public class LightFirmwareService {
	
	@Resource(name="lightFirmwareDao")
	private LightFirmwareDao lightFirmwareDao;
	
	public List<LightFirmwareBean> queryLightFirmwareList(Integer userId) {
		List<LightFirmwareBean> lightFirmwareBeanList = lightFirmwareDao.queryLightFirmwareList(userId);
		
		if(lightFirmwareBeanList==null || lightFirmwareBeanList.size()<1) {
			return lightFirmwareBeanList;			
		}
		
		for(LightFirmwareBean lightFirmwareBean : lightFirmwareBeanList) {
			lightFirmwareBean.setFirmwareTimeStr(DateUtil.timeFormat(lightFirmwareBean.getFirmwareTime()));
		}
		
		return lightFirmwareBeanList;
	}
	
	public boolean addOrUpdateLightFirmware(LightFirmware lightFirmware) {
		LightFirmware lastLightFirmware = lightFirmwareDao.queryLightFirmwareByLightId(lightFirmware.getLightId());
		if(lastLightFirmware==null) {
			return lightFirmwareDao.addLightFirmware(lightFirmware);
		}else {
			// 信息比较
			if(!lastLightFirmware.getFirmwareVersion().equals(lightFirmware.getFirmwareVersion())
					|| !lastLightFirmware.getHardwareVersion().equals(lightFirmware.getHardwareVersion())
					|| !lastLightFirmware.getProtocalVersion().equals(lightFirmware.getProtocalVersion())
					|| !lastLightFirmware.getFirmwareTime().equals(lightFirmware.getFirmwareTime())
					|| !lastLightFirmware.getImei().equals(lightFirmware.getImei())
					|| !lastLightFirmware.getIccid().equals(lightFirmware.getIccid())) {
				return lightFirmwareDao.updateLightFirmware(lightFirmware);
			}
		}
		
		return true;
	}
	

}
